struct ListNode {  
    val: i32,  
    next: Option<Box<ListNode>>,  
}  

struct ListIterator {  
    current: Option<Box<ListNode>>,  
}  

impl Iterator for ListIterator {  
    type Item = i32;  

    fn next(&mut self) -> Option<Self::Item> {  
        // TODO: 遍历链表节点  
        // 取出当前节点并转移所有权
        let node = self.current.take()?;
        // 移动指针到下一个节点
        self.current = node.next;
        // 返回当前节点值
        Some(node.val)
        //None
    }  
}  

#[test]  
fn test_list_iter() {  
    let list = Some(Box::new(ListNode {  
        val: 1,  
        next: Some(Box::new(ListNode { val: 2, next: None })),  
    }));  
    let iter = ListIterator { current: list };  
    assert_eq!(iter.collect::<Vec<_>>(), vec![1, 2]);  
}  

// 添加main函数以通过编译
fn main() {
    // 演示用法
    let list = Some(Box::new(ListNode {
        val: 3,
        next: Some(Box::new(ListNode { val: 1, next: None })),
    }));
    let sorted: Vec<_> = ListIterator { current: list }.collect();
    println!("Sorted values: {:?}", sorted);
}